package cn.com.yx.ms.controller;

import cn.com.yx.ms.application.web.ApiPager;
import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.TestModel;
import cn.com.yx.ms.domain.TreeModel;
import cn.com.yx.ms.domain.dbmodel.SysLog;
import cn.com.yx.ms.domain.dbmodel.SysRole;
import cn.com.yx.ms.service.ILogService;
import cn.com.yx.ms.service.ITestService;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.configuration.BaseConstant;
import cn.yx.framework.util.DateUtil;
import cn.yx.framework.view.ApiResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;
import java.util.concurrent.TimeUnit;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "logger")
@Api(value = "日志接口", description = "用于日志资源获取接口")
public class LoggerController {

    private static final Logger logger = LoggerFactory.getLogger(LoggerController.class);

    @Resource
    private ILogService iLogService;

    @ResponseBody
    @RequestMapping(value = "page", method = RequestMethod.GET)
    @ApiOperation("获取带分页的业务日志数据")
    public ApiResult find(@ApiQuery ApiQueryBase query, HttpServletRequest request) {

        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        query.getConditions().put("type", request.getParameter("type"));
        query.getConditions().put("module", request.getParameter("module"));
        query.getConditions().put("content", request.getParameter("content"));
        query.getConditions().put("starttime", request.getParameter("starttime"));
        query.getConditions().put("endtime", request.getParameter("endtime"));
        query.getConditions().put("cname", request.getParameter("cname"));

        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        Page<SysLog> page = (Page<SysLog>) iLogService.find(query);
        ApiPager<SysLog> apiPager = new ApiPager<SysLog>(query.getPs(),
                query.getPi(),
                page.getTotal(),
                page.getResult());

        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", apiPager);

    }

    @ResponseBody
    @RequestMapping(value = "compush", method = RequestMethod.GET)
    @ApiOperation("获取上行文件的日志文件夹名称")
    public ApiResult findCompush() {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, iLogService.findCommandPusthLogs("/usr/local/nginx/resources/logs"));
    }

    @ResponseBody
    @RequestMapping(value = "compushlogs", method = RequestMethod.GET)
    @ApiOperation("获取的上行文件的日志文件名称")
    public ApiResult findCompushiLogs(@ApiQuery ApiQueryBase query, HttpServletRequest request) {
        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        String path = request.getParameter("compushlogs");
        if (path.toString() == null || path.equals("")) {
            path = "/usr/local/nginx/resources/logs";
        } else {
            path = "/usr/local/nginx/resources/" + path;
        }
        query.getConditions().put("compushlogs", path);
        List<TreeModel> treeModelList = iLogService.findComPushLogs(query);
        //分页处理
        int pi = Integer.parseInt(request.getParameter("pi").toString());
        int ps = Integer.parseInt(request.getParameter("ps").toString());
        int orderBegin = (pi - 1) * ps;
        int orderEnd = pi * ps;
        if (orderEnd > treeModelList.size()) {
            orderEnd = treeModelList.size();
        }
        List<TreeModel> treeModelList1 = new ArrayList<TreeModel>();
        for (int i = orderBegin; i < orderEnd; i++) {
            treeModelList1.add(treeModelList.get(i));
        }
        ApiPager<TreeModel> apiPager = new ApiPager<TreeModel>(query.getPs(),
                query.getPi(),
                treeModelList.size(),
                treeModelList1);
        //System.out.println("长度:" + apiPager.getList().size());
        //System.out.println(treeModelList1.size());
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", apiPager);
    }

    @ResponseBody
    @RequestMapping(value = "compushlog", method = RequestMethod.GET)
    @ApiOperation("读取单个日志文件的信息")
    public ApiResult findSingleLog(@ApiQuery ApiQueryBase query, HttpServletRequest request) {
        //System.out.println("id:"+request.getParameter("id").toString());
        String path = request.getParameter("id").toString();
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, iLogService.findLogBypath(path));
    }


}